package kotan.view;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import kotan.AppEngineEnv;
import kotan.view.common.FileField;

public class StartupPanel extends JPanel {

    private static final long serialVersionUID = 1L;
    AppEngineEnv env;
    EmbeddedPanel embeddedPanel;
    JTextField hostField;
    JFormattedTextField portField;

    public StartupPanel() {
        initComponents();
        this.setBorder(BorderFactory.createEmptyBorder(2, 5, 3, 5));
        this.setMinimumSize(new Dimension(400, 300));
        this.setPreferredSize(new Dimension(600, 400));
    }

    private void initComponents() {
        // mode
        JLabel modeLabel = new JLabel();
        modeLabel.setText("Server mode: ");
        JRadioButton embeddedButton = new JRadioButton("Local(Embedded)");
        embeddedButton.setSelected(true);
        JRadioButton localButton = new JRadioButton("Local Server");
        localButton.setEnabled(false);
        JRadioButton productionButton = new JRadioButton("Production Server");
        productionButton.setEnabled(false);
        ButtonGroup modeGroup = new ButtonGroup();
        modeGroup.add(embeddedButton);
        modeGroup.add(localButton);
        modeGroup.add(productionButton);
        embeddedPanel = new EmbeddedPanel();
        // host
        JLabel hostLable = new JLabel();
        hostLable.setText("Host name:");
        hostField = new JTextField(16);
        // port
        JLabel portLable = new JLabel();
        portLable.setText("Port:");
        portField = new JFormattedTextField(new DecimalFormat("###0"));
        // Start / Cancel
        JButton startButton = new JButton();
        startButton.setText("Start");
        startButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                onStart();
            }
        });
        // Layout
        this.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 1;
        gbc.weightx = 0.0;
        gbc.weighty = 0.0;
        gbc.anchor = GridBagConstraints.EAST;
        gbc.fill = GridBagConstraints.NONE;
        gbc.insets = new Insets(3, 5, 2, 5);
        add(modeLabel, gbc);
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.gridwidth = 1;
        gbc.weightx = 0.0;
        gbc.weighty = 0.0;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.fill = GridBagConstraints.NONE;
        gbc.insets = new Insets(3, 5, 2, 5);
        add(embeddedButton, gbc);
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.gridwidth = 1;
        gbc.weightx = 0.0;
        gbc.weighty = 0.0;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.fill = GridBagConstraints.NONE;
        gbc.insets = new Insets(3, 5, 2, 5);
        add(localButton, gbc);
        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.gridwidth = 1;
        gbc.weightx = 0.0;
        gbc.weighty = 0.0;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.fill = GridBagConstraints.NONE;
        gbc.insets = new Insets(3, 5, 2, 5);
        add(productionButton, gbc);
        // host / port
        gbc.gridx = 0;
        gbc.gridy = 3;
        gbc.gridwidth = 1;
        gbc.weightx = 0.0;
        gbc.weighty = 0.0;
        gbc.anchor = GridBagConstraints.EAST;
        gbc.fill = GridBagConstraints.NONE;
        gbc.insets = new Insets(3, 5, 2, 5);
        add(hostLable, gbc);
        gbc.gridx = 1;
        gbc.gridy = 3;
        gbc.gridwidth = 1;
        gbc.weightx = 0.0;
        gbc.weighty = 0.0;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.fill = GridBagConstraints.NONE;
        gbc.insets = new Insets(3, 5, 2, 5);
        add(hostField, gbc);
        gbc.gridx = 0;
        gbc.gridy = 4;
        gbc.gridwidth = 1;
        gbc.weightx = 0.0;
        gbc.weighty = 0.0;
        gbc.anchor = GridBagConstraints.EAST;
        gbc.fill = GridBagConstraints.NONE;
        gbc.insets = new Insets(3, 5, 2, 5);
        add(portLable, gbc);
        gbc.gridx = 1;
        gbc.gridy = 4;
        gbc.gridwidth = 1;
        gbc.weightx = 0.0;
        gbc.weighty = 0.0;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.fill = GridBagConstraints.NONE;
        gbc.insets = new Insets(3, 5, 2, 5);
        add(portField, gbc);
        // embedded
        // TODO CardLayout
        gbc.gridx = 0;
        gbc.gridy = 5;
        gbc.gridwidth = 2;
        gbc.weightx = 0.0;
        gbc.weighty = 0.0;
        gbc.anchor = GridBagConstraints.NORTHWEST;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(5, 15, 5, 10);
        add(embeddedPanel, gbc);

        // StartButton
        gbc.gridx = 0;
        gbc.gridy = 6;
        gbc.gridwidth = 2;
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;
        gbc.anchor = GridBagConstraints.NORTHEAST;
        gbc.fill = GridBagConstraints.NONE;
        gbc.insets = new Insets(3, 5, 2, 5);
        add(startButton, gbc);
    }


    void setAppEngineEnv(AppEngineEnv env) {
        this.env = env;
        hostField.setText(env.getHost());
        portField.setValue(env.getPort());
        embeddedPanel.sdkHomeField.setText(env.getSdkHome());
        embeddedPanel.appIdField.setText(env.getApplicationId());
        embeddedPanel.appVersionField.setText(env.getApplicationVersion());
        embeddedPanel.datastorePathField.setText((env.getDatastorePath()));
    }
   
    /**
     * invoke when 'Start' button is clicked.
     */
    protected void onStart() {
        env.setHost(hostField.getText());
        env.setPort((Integer) portField.getValue());
        env.setSdkHome(embeddedPanel.sdkHomeField.getText());
        env.setApplicationId(embeddedPanel.appIdField.getText());
        env.setApplicationVersion(embeddedPanel.appVersionField.getText());
        env.setDatastorePath(embeddedPanel.datastorePathField.getText());
    }

    protected void onCancel() {
        // TODO Auto-generated method stub
    }

    class EmbeddedPanel extends JPanel {
        private static final long serialVersionUID = 1L;
        FileField sdkHomeField;
        JTextField appIdField;
        JTextField appVersionField;
        FileField datastorePathField;

        EmbeddedPanel() {
            initComponents();
            this.setBorder(BorderFactory.createTitledBorder("Embedded"));
        }

        private void initComponents() {
            // SDK home
            JLabel sdkHomeLabel = new JLabel();
            sdkHomeLabel.setText("AppEngine SDK home:");
            sdkHomeField = new FileField(20, JFileChooser.DIRECTORIES_ONLY);
            // Application ID
            JLabel appIdLabel = new JLabel();
            appIdLabel.setText("Application ID:");
            appIdField = new JTextField(16);
            // Application Version
            JLabel appVersionLabel = new JLabel();
            appVersionLabel.setText("Version:");
            appVersionField = new JTextField(8);
            // Datastore path
            JLabel datastorePathLabel = new JLabel();
            datastorePathLabel.setText("Datastore path:");
            datastorePathField = new FileField(20, JFileChooser.FILES_ONLY);
            // Layout
            this.setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            // sdkHomeLabel
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.gridwidth = 1;
            gbc.weightx = 0.0;
            gbc.weighty = 0.0;
            gbc.anchor = GridBagConstraints.EAST;
            gbc.fill = GridBagConstraints.NONE;
            gbc.insets = new Insets(3, 5, 2, 5);
            add(sdkHomeLabel, gbc);
            gbc.gridx = 1;
            gbc.gridy = 0;
            gbc.gridwidth = 1;
            gbc.weightx = 0.0;
            gbc.weighty = 0.0;
            gbc.anchor = GridBagConstraints.WEST;
            gbc.fill = GridBagConstraints.NONE;
            gbc.insets = new Insets(3, 5, 2, 5);
            add(sdkHomeField, gbc);
            // Application ID
            gbc.gridx = 0;
            gbc.gridy = 1;
            gbc.gridwidth = 1;
            gbc.weightx = 0.0;
            gbc.weighty = 0.0;
            gbc.anchor = GridBagConstraints.EAST;
            gbc.fill = GridBagConstraints.NONE;
            gbc.insets = new Insets(3, 5, 2, 5);
            add(appIdLabel, gbc);
            gbc.gridx = 1;
            gbc.gridy = 1;
            gbc.gridwidth = 1;
            gbc.weightx = 0.0;
            gbc.weighty = 0.0;
            gbc.anchor = GridBagConstraints.WEST;
            gbc.fill = GridBagConstraints.NONE;
            gbc.insets = new Insets(3, 5, 2, 5);
            add(appIdField, gbc);
            // Application ID
            gbc.gridx = 0;
            gbc.gridy = 2;
            gbc.gridwidth = 1;
            gbc.weightx = 0.0;
            gbc.weighty = 0.0;
            gbc.anchor = GridBagConstraints.EAST;
            gbc.fill = GridBagConstraints.NONE;
            gbc.insets = new Insets(3, 5, 2, 5);
            add(appVersionLabel, gbc);
            gbc.gridx = 1;
            gbc.gridy = 2;
            gbc.gridwidth = 1;
            gbc.weightx = 0.0;
            gbc.weighty = 0.0;
            gbc.anchor = GridBagConstraints.WEST;
            gbc.fill = GridBagConstraints.NONE;
            gbc.insets = new Insets(3, 5, 2, 5);
            add(appVersionField, gbc);
            // datastorePathField
            gbc.gridx = 0;
            gbc.gridy = 3;
            gbc.gridwidth = 1;
            gbc.weightx = 0.0;
            gbc.weighty = 0.0;
            gbc.anchor = GridBagConstraints.EAST;
            gbc.fill = GridBagConstraints.NONE;
            gbc.insets = new Insets(3, 5, 2, 5);
            add(datastorePathLabel, gbc);
            gbc.gridx = 1;
            gbc.gridy = 3;
            gbc.gridwidth = 1;
            gbc.weightx = 0.0;
            gbc.weighty = 0.0;
            gbc.anchor = GridBagConstraints.WEST;
            gbc.fill = GridBagConstraints.NONE;
            gbc.insets = new Insets(3, 5, 2, 5);
            add(datastorePathField, gbc);
        }

    }

}
